package com.linsen.core.lock.config;

import com.linsen.core.common.annotation.AutoConfiguration;
import com.linsen.core.lock.RedissonLock;
import com.linsen.core.lock.RedissonManager;
import com.linsen.core.lock.aspect.DistributedLockHandler;
import com.linsen.core.lock.props.LinsenRedissionProperties;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;

/**
 * Redisson自动化配置
 * @author zhaoyong
 * @date 2023/3/21
 */
@Slf4j
@AutoConfiguration
@ConditionalOnClass(Redisson.class)
@EnableConfigurationProperties(LinsenRedissionProperties.class)
public class LinsenLockAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    @Order(value = 2)
    public RedissonLock redissonLock(RedissonManager redissonManager) {
        RedissonLock redissonLock = new RedissonLock();
        redissonLock.setRedissonManager(redissonManager);
        log.info("[RedissonLock]组装完毕");
        return redissonLock;
    }

    @Bean
    @ConditionalOnMissingBean
    @Order(value = 1)
    public RedissonManager redissonManager(LinsenRedissionProperties redissonProperties) {
        RedissonManager redissonManager =
                new RedissonManager(redissonProperties);
        log.info("[RedissonManager]组装完毕,当前连接方式:" + redissonProperties.getType() +
                ",连接地址:" + redissonProperties.getAddress());
        return redissonManager;
    }

    @Bean
    public DistributedLockHandler distributedLockHandler(RedissonLock redissonLock){
        return new DistributedLockHandler(redissonLock);
    }
}
